package com.yan.yjhTest.io.serialize;

import java.io.*;

public class Test {

    public static void main(String[] args) {
        try {
            //serialize();//���л�
            deserialize();//�����л�
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public static void test1() {
        try {
            //serialize();//���л�
            deserialize();//�����л�
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * ���л�
     * @throws IOException
     */
    public static void serialize( ) throws IOException {

        Student student = new Student();
        student.setName("CodeSheep");
        student.setAge(18);
        student.setScore( 1000 );

        ObjectOutputStream objectOutputStream =
                new ObjectOutputStream( new FileOutputStream( new File("student.txt") ) );
        objectOutputStream.writeObject( student );
        objectOutputStream.close();

        System.out.println("���л��ɹ����Ѿ�����student.txt�ļ�");
        System.out.println("==============================================");
    }

    /**
     * �����л�
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void deserialize( ) throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream =
                new ObjectInputStream( new FileInputStream( new File("student.txt") ) );
        Student student = (Student) objectInputStream.readObject();
        objectInputStream.close();

        System.out.println("�����л����Ϊ��");
        System.out.println( student );
    }
}
